package bb.lanxing.activity.launch

import android.content.Intent
import android.os.CountDownTimer
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import bb.lanxing.App
import bb.lanxing.MainActivity
import bb.lanxing.R
import bb.lanxing.activity.base.BaseActivityViewBinding
import bb.lanxing.common.config.Constants
import bb.lanxing.databinding.ActivityLaunchBinding
import bb.lanxing.manager.SharedManager
import bb.lanxing.utils.IntentUtils
import bb.lanxing.view.dialog.CommonDialogFragment
import com.blankj.utilcode.util.ConvertUtils


class LaunchActivity : BaseActivityViewBinding<ActivityLaunchBinding>() {
    private var isAgreement = false

    private fun dismiss() {
        object : CountDownTimer(300L, 100L) {
            override fun onTick(tick: Long) {}

            override fun onFinish() {
                this@LaunchActivity.launchApp()
            }
        }.start()
    }

    override fun getViewBinding(): ActivityLaunchBinding {
        return ActivityLaunchBinding.inflate(layoutInflater)
    }

    override fun getLayout(): Int {
        return R.layout.activity_launch
    }

    override fun initData() {
        dismiss()
    }

    override fun initView() {
        window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or 512
    }

    override fun loadData() {
        isAgreement = SharedManager.getInstance().isUseAgreementAgree
    }

    @Synchronized
    fun launchApp() {
        if (isAgreement) {
            goHomePage()
        } else {
            showAgreementDialog()
        }
    }

    private fun showAgreementDialog() {
        val dialog = CommonDialogFragment(R.layout.dialog_agreement, object : CommonDialogFragment.ViewListener {
            override fun bindView(v: View, dialogFragment: DialogFragment) {
                val cancelTv = v.findViewById<TextView>(R.id.disagree)
                val sureTv = v.findViewById<TextView>(R.id.agree)
                val content = v.findViewById<TextView>(R.id.agreement)
                content?.apply {
                    val text = text
                    val spannable = SpannableString(text)
                    val policySpan = object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            IntentUtils.gotoWebBrowser(this@LaunchActivity, Constants.URL_PRIVACY_AGREEMENT, null)
                        }

                        override fun updateDrawState(ds: TextPaint) {
                            super.updateDrawState(ds)
                            ds.color = getColor(R.color.global_blue_color) // 设置链接颜色
                            ds.isUnderlineText = false // 可以取消下划线
                        }
                    }
                    val serviceSpan = object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            IntentUtils.gotoWebBrowser(this@LaunchActivity, Constants.URL_USER_AGREEMENT, null)
                        }

                        override fun updateDrawState(ds: TextPaint) {
                            super.updateDrawState(ds)
                            ds.color = getColor(R.color.global_blue_color) // 设置链接颜色
                            ds.isUnderlineText = false // 可以取消下划线
                        }
                    }
                    val policy = StringBuilder(getString(R.string.character_left)).append(getString(R.string.dialog_privacy_policy)).append(getString(R.string.character_right)).toString()
                    val service = StringBuilder(getString(R.string.character_left)).append(getString(R.string.dialog_service_agreement)).append(getString(R.string.character_right)).toString()
                    val start1 = text.indexOf(policy)
                    if (-1 != start1) {
                        val end = start1 + policy.length
                        spannable.setSpan(policySpan, start1, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                    val start2 = text.indexOf(service)
                    if (-1 != start2) {
                        val end = start2 + service.length
                        spannable.setSpan(serviceSpan, start2, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                    if (-1 != start1 && -1 != start2) {
                        this.text = spannable
                        (LinkMovementMethod.getInstance()).also { this.movementMethod = it }
                    }
                }
                cancelTv?.apply {
                    setOnClickListener {
                        finish()
                    }
                }
                sureTv?.apply {
                    setOnClickListener {
                        dialogFragment.dismissAllowingStateLoss()
                        SharedManager.getInstance().setUserAgreement(true)
                        App.getInstance().initAfterUserAgree()
                        goHomePage()
                    }
                }
            }
        }).setMargin(ConvertUtils.dp2px(10f))
        dialog.setCancelable(false)
        dialog.show(supportFragmentManager)
    }

    private fun goHomePage() {
        val intent = Intent()
        intent.setClass(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }
}